﻿#include "productlibrarystandarditemmodel.h"
#include "tools.h"

ProductLibraryStandardItemModel::ProductLibraryStandardItemModel(QObject *parent) : QStandardItemModel(parent)
{
    header<<"商品ID"
         <<"型号"
        <<"型号拼音码"
       <<"名称"
      <<"名称拼音码"
     <<"单位"
    <<"下限"
    <<"上限"
    <<"库存数量"
    <<"净价"
    <<"三包库存"
    <<"移库数量"
    <<"货号"
    <<"品牌"
    <<"产地"
    <<"规格"
    <<"条型码"
    <<"备注"
    <<"指导价格1"
    <<"指导价格2"
    <<"一级类别"
    <<"二级类别";

    sanbaoCountIndex = 10;
    inPriceIndex = 9;

    setHorizontalHeaderLabels(header);
}

bool ProductLibraryStandardItemModel::setData(const QModelIndex &index, const QVariant &value, int role){
    if(index.column() != 11){
        return false;
    }

    int saobaoMoveCount = value.toInt();
    if(saobaoMoveCount<0) saobaoMoveCount = 0;

    Product *product = products[index.row()];
    product->sanbaoMoveCount = saobaoMoveCount;
    QStandardItemModel::setData(index, saobaoMoveCount, role);

    return true;
}

QVariant ProductLibraryStandardItemModel::data(const QModelIndex &index, int role) const{
    QVariant value = QStandardItemModel::data(index, role);
    //设置内容居中
    if(role==Qt::TextAlignmentRole){
        return  Qt::AlignCenter;
    }

    //设置内容颜色
    if (role == Qt::TextColorRole && (index.column() == 11))
        return QVariant::fromValue(QColor(Qt::blue));
    return value;
}

void ProductLibraryStandardItemModel::insertProduct(Product &product, int defaultCount){
    QList<QStandardItem*> rowData;

    product.sanbaoMoveCount = defaultCount;
    rowData.append(new QStandardItem(product.productId));
    rowData.append(new QStandardItem(product.type));
    rowData.append(new QStandardItem(product.typePinyin));
    rowData.append(new QStandardItem(product.name));
    rowData.append(new QStandardItem(product.namePinyin));
    rowData.append(new QStandardItem(product.unit));
    rowData.append(new QStandardItem(QString("%1").arg(product.lowerLimit)));
    rowData.append(new QStandardItem(QString("%1").arg(product.upperLimit)));
    rowData.append(new QStandardItem(QString("%1").arg(product.count)));
    rowData.append(new QStandardItem(QString::number(product.inPrice, 'f', 2)));
    rowData.append(new QStandardItem(QString("%1").arg(product.sanbao)));
    rowData.append(new QStandardItem(QString("%1").arg(product.sanbaoMoveCount)));
    rowData.append(new QStandardItem(product.productNum));
    rowData.append(new QStandardItem(product.brand));
    rowData.append(new QStandardItem(product.originPlace));
    rowData.append(new QStandardItem(product.specifications));
    rowData.append(new QStandardItem(product.barCode));
    rowData.append(new QStandardItem(product.remark));
    rowData.append(new QStandardItem(QString::number(product.guidePrice1, 'f', 2)));
    rowData.append(new QStandardItem(QString::number(product.guidePrice2, 'f', 2)));
    rowData.append(new QStandardItem(product.primaryDirectory));
    rowData.append(new QStandardItem(product.secondaryDirectory));

    products.append(&product);
    insertRow(rowCount()-1, rowData);
}

void ProductLibraryStandardItemModel::insertProduct(QList<Product*> products, int defaultCount){
    int i=0;

    for(Product *product:products){
        if(product->checkExists(this->products)){
            i++;
        }else{
            insertProduct(*product, defaultCount);
        }
    }

    if(i != 0){
        Tools::showRepeatProductMsg();
    }

    refreshTotalValue();
}

void ProductLibraryStandardItemModel::reset(){
    clear();
    products.clear();
    setHorizontalHeaderLabels(header);
}

QList<Product*> ProductLibraryStandardItemModel::currentProducts(){
    return products;
}
/**
 * 刷新合计
 * @brief ProductLibraryStandardItemModel::refreshTotalValue
 */
void ProductLibraryStandardItemModel::refreshTotalValue(){
    int totalCount = 0;
    double totalMoney = 0.0;
    for(int i=0;i<rowCount();i++){
        QStandardItem *sanbaoCountItem = item(i, sanbaoCountIndex);
        QStandardItem *inPriceItem = item(i, inPriceIndex);
        if(sanbaoCountItem == nullptr || inPriceItem == nullptr){
            break;
        }
        int sanbaoCount = sanbaoCountItem->text().toInt();
        double inPrice = inPriceItem->text().toDouble();
        totalCount += sanbaoCount;
        totalMoney += sanbaoCount*inPrice;
    }

    emit updateTotalValue(totalCount, totalMoney);
}
